package net.sf.midollo.io;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import net.sf.midollo.io.ionull.NullInputStream;
import net.sf.midollo.io.ionull.NullOutputStream;

public class IOUtils {

  public static final InputStream NULL_INPUT = new NullInputStream();
  public static final OutputStream NULL_OUTPUT = new NullOutputStream();

  public static DataInputStream toDataInputStream(InputStream is) {
    if (is instanceof DataInputStream)
      return (DataInputStream)is;
    return new DataInputStream(is);
  }

  public static DataOutputStream toDataOutputStream(OutputStream os) {
    if (os instanceof DataOutputStream)
      return (DataOutputStream)os;
    return new DataOutputStream(os);
  }

  public static IOStream[] connectFullDuplex(IOStream ioClient, IOStream ioServer) throws IOException {
    IOStream ioRequest = connectRequest(ioClient, ioServer);
    IOStream ioResponse = connectResponse(ioClient, ioServer);
    return new IOStream[] { ioRequest, ioResponse };
  }

  public static IOStream connectRequest(IOStream ioClient, IOStream ioServer) throws IOException {
    return new DefaultIOStream(ioClient.getInput(), ioServer.getOutput());
  }

  public static IOStream connectResponse(IOStream ioClient, IOStream ioServer) throws IOException {
    return new DefaultIOStream(ioServer.getInput(), ioClient.getOutput());
  }
}
